import { createRouter, createWebHistory } from 'vue-router'


const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'Login',
      component: ()=>import('../views/login.vue')
    },
    {
      path: '/main',
      name:'main',
      component: () => import('../views/main.vue'),
      redirect: '/dashBoard',
      children: [
        {
          path: '/dashBoard',
          name: 'dashBoard',
          component: () => import('../views/dashBoard/dashBoard.vue')
        },
        {
          path: '/server',
          name: 'server',
          component: () => import('../views/server/server.vue')
        },

        {
          path: '/article',
          name: 'article',
          component: () => import('../views/article/article.vue')
        },
        {
          path: '/articleadd',
          name: 'articleadd',
          component: () => import('../views/article/articleAdd.vue')
        },
        {
          path: '/editarticle/:id',
          name: 'editarticle',
          component: () => import('../views/article/articleEdit.vue')
        },
        {
          path: '/useredit',
          name: 'useredit',
          component: () => import('../views/peopleedit/user.vue')
        },
      ]
    },
    {
      path: '/nf',
      name: '404',
      component: () => import('../views/status/404.vue'),
    }
  ]
})
//前置守卫,查看token
router.beforeEach((to, from) => {
  //token和Login
  console.log(sessionStorage.getItem('token'))
  if (!sessionStorage.getItem('token') && to.name != 'Login') {
    ElMessage.error({
      message: '请先登录!',
      center:true
    })
    return {name:'Login'}
  }
})


export default router
